using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;

#pragma warning disable IDE1006 // 命名样式

namespace QNLua.CommonData
{
	public enum LuaCommonDataType
	{
		PutSectStone,
		TraceTask,
		DoTask,
		GotoNPC,
		GotoNPCShopType,
		GotoLocation,
		CheckAutoBattle,
		StartAutoBattle,
		DoAction,
		ItemTipWindow,
		SkillTipWindow,
		SkillTipHandleBtnCfg,
		EnterSkillPublicCD,
		StartRecharge,
		ItemTipHandleBtnCfg,
		RedDotSystem,
		RedDot,
		CustomMap,
		QuitHandle,
		MapLeftTime,
		PCKeyMap,
		KaiGuan,
		SkillEnhanceConfig,
		GameBoxUrl,
		RequestOpenView,
		CacheViews,
		Posture,
		MapBuffNameConfig
	}

	public class LuaCommonDataParamsConverter : JsonConverter
	{
		private static LuaCommonDataParams Create(LuaCommonDataType type)
		{
			switch (type)
			{
				case LuaCommonDataType.SkillTipWindow: return new SkillTipWindowParams();
				case LuaCommonDataType.ItemTipWindow: return new ItemTipWindowParams();
				case LuaCommonDataType.PutSectStone: return new PutSectStoneParams();
				case LuaCommonDataType.TraceTask: return new TraceTaskParams();
				case LuaCommonDataType.DoTask: return new DoTaskParams();
				case LuaCommonDataType.GotoNPC: return new GotoNPCParams();
				case LuaCommonDataType.GotoNPCShopType: return new GotoNPCShopTypeParams();
				case LuaCommonDataType.GotoLocation: return new GotoLocationParams();
				case LuaCommonDataType.CheckAutoBattle: return new CheckAutoBattleParams();
				case LuaCommonDataType.StartAutoBattle: return new StartAutoBattleParams();
				case LuaCommonDataType.DoAction: return new DoActionParams();
				case LuaCommonDataType.RedDot: return new RedDotParams();
				case LuaCommonDataType.CustomMap: return new CustomMapParams();
				case LuaCommonDataType.QuitHandle: return new QuitHandleParams();
				case LuaCommonDataType.MapLeftTime: return new MapLeftTimeParams();
				case LuaCommonDataType.PCKeyMap: return new PCKeyMapParams();
				case LuaCommonDataType.SkillEnhanceConfig: return new SkillEnhanceConfigParams();
				case LuaCommonDataType.GameBoxUrl: return new GameBoxUrlParams();
				case LuaCommonDataType.RequestOpenView: return new RequestOpenViewParams();
				case LuaCommonDataType.CacheViews: return new CacheViewsParams();
				case LuaCommonDataType.Posture: return new PostureParams();
				case LuaCommonDataType.MapBuffNameConfig: return new MapBuffNameConfigParams();
				case LuaCommonDataType.SkillTipHandleBtnCfg:
					return new SkillTipHandleBtnCfgParams();
				case LuaCommonDataType.EnterSkillPublicCD:
					return new EnterSkillPublicCDParams();
				case LuaCommonDataType.StartRecharge:
					return new StartRechargeParams();
				case LuaCommonDataType.ItemTipHandleBtnCfg:
					return new ItemTipHandleBtnCfgParams();
				case LuaCommonDataType.RedDotSystem:
					return new RedDotSystemParams();
				case LuaCommonDataType.KaiGuan:
					return new KaiGuanParams();
			}
			return null;
		}

		public override bool CanConvert(Type objectType)
		{
			return typeof(LuaCommonDataParams).IsAssignableFrom(objectType);
		}

		public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
		{
			var jo = JObject.Load(reader);

			var typeToken = jo["type"];
			if (typeToken == null)
			{
				throw new JsonSerializationException($"no type");
			}

			var typeStr = typeToken.ToString();
			if (Enum.TryParse<LuaCommonDataType>(typeStr, false, out var enumVal))
			{
				var target = Create(enumVal);
				if (target != null)
				{
					serializer.Populate(jo.CreateReader(), target);
					return target;
				}
			}


			throw new JsonSerializationException($"no type");
		}

		public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
		{
			// 直接使用默认序列化逻辑
			serializer.Serialize(writer, value);
		}
	}

	[JsonConverter(typeof(LuaCommonDataParamsConverter))]
	public class LuaCommonDataParams
	{
		[JsonProperty("type")]
		public LuaCommonDataType? Type { get; set; }
	}
}
